<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="t" tagdir="/WEB-INF/tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="joda" uri="http://www.joda.org/joda/time/tags"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<t:masterpage>
	<jsp:body>
        <c:url value="/operations" var="operationsUrl" />
        <c:url value="/donations" var="donationsUrl" />
        <c:url value="/static/img/interface/closed.png" var="imageClosed" />
        <c:set var="timezone" scope="session" value="Europe/Moscow" />
        
        <h2>Operations history</h2>
        
        <div class="search_params horizontal_pad">
        <p class="info">Search by</p>
        <form:form method="post" action="${operationsUrl}.html"
				modelAttribute="operationHistoryPageModel">
            <table>
                <tr>
                    <th valign="top"><form:label path="humanResources.id">HR</form:label></th>
                    <td>
                        <form:select path="humanResources.id">
                            <c:forEach var="person" items="${operationHistoryPageModel.allPeople}">
                                <form:option value="${person.id}">
                                    <c:out value="${person.firstName} ${person.lastName}" />
                                </form:option>
                            </c:forEach>
                        </form:select><br />
                        <form:errors class="text-error" path="humanResources.id" />
                    </td>
                </tr>
                <tr>
                    <th valign="top"><form:label path="receiptor.id">Receiptor</form:label></th>
                    <td>
                        <form:select path="receiptor.id">
                            <c:forEach var="person" items="${operationHistoryPageModel.allPeople}">
                                <form:option value="${person.id}">
                                    <c:out value="${person.firstName} ${person.lastName}" />
                                </form:option>
                            </c:forEach>
                        </form:select><br />
                        <form:errors class="text-error" path="receiptor.id" />
			       </td>
                </tr>                
                <tr>
                    <th valign="top"><form:label path="donator.id">Donator</form:label></th>
                    <td>
                        <form:select path="donator.id">
                            <c:forEach var="person" items="${operationHistoryPageModel.allPeople}">
                                <form:option value="${person.id}">
                                        <c:out value="${person.firstName} ${person.lastName}" />
                                </form:option>
                            </c:forEach>
                        </form:select><br />
                        <form:errors class="text-error" path="donator.id" />
                    </td>
                </tr>
                <tr>
                    <th valign="top"><form:label path="selectedYear">Year</form:label></th>
                    <td><form:select path="selectedYear">
                    <c:forEach var="year"
                        items="${operationHistoryPageModel.allYears}">
                            <form:option value="${year}">
                                <c:out value="${year}" />
                            </form:option>
                        </c:forEach>
                    </form:select><br />
                    <form:errors path="selectedYear" /></td>
                </tr>
                <tr>
                    <th valign="top"><form:label path="startDate">Start date</form:label></th>
                    <td><form:input path="startDate" /><br />
                    <form:errors class="text-error" path="startDate" /></td>
                </tr>
                <tr>
                    <th valign="top"><form:label path="endDate">End date</form:label></th>
                    <td><form:input path="endDate" /><br />
                    <form:errors class="text-error" path="endDate" /></td>
                </tr>
                <tr>
                    <td colspan="3">
                        <div class="form-actions">
                            <input class="btn btn-primary" type="submit"
                                value="Search" />
                        </div>
                    </td>
                </tr>
            </table>
        </form:form>
        </div>
        
        <div class="search_results">
        <c:if test="${operationHistoryPageModel.operations != null && !empty operationHistoryPageModel.operations}">
            <table id="staff" class="table table-hover">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Donator</th>
                        <th>Receiptor</th>
                        <th>Amount</th>
                        <th>Year</th>
                        <th>HR</th>
                    </tr>
                </thead>
                <tbody>
                    <c:forEach items="${operationHistoryPageModel.operations}"
                        var="operation">
                        <tr>
                            <td>
                                <joda:format value="${operation.date}" pattern="dd.MM.yyyy, HH:mm" dateTimeZone="${timezone}" />
                            </td>
                            <td>
                                ${operation.donator.firstName} ${operation.donator.lastName}
                            </td>
                            <td>
                            	<a href="${donationsUrl}/${operation.receiptor.id}.html">${operation.receiptor.firstName} ${operation.receiptor.lastName}</a>
                            	<c:if test="${operation.receiptor.fired}">
                            		<img title="Closed (person is fired)" src="${imageClosed}" />
                            	</c:if>
                            </td>
                            <td>
                                ${operation.amount}
                            </td>
                            <td>
                                ${operation.year}
                            </td>
                            <td>
                                ${operation.humanResources.firstName} ${operation.humanResources.lastName}
                            </td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table> 
        </c:if>  
        </div>
    </jsp:body>
</t:masterpage>